package com.plum.admin.config.security;

import com.plum.admin.config.properties.IgnoredUrlsProperties;
import com.plum.admin.config.security.filter.AuthenticationFailureHandler;
import com.plum.admin.config.security.filter.AuthenticationSuccessHandler;
import com.plum.admin.config.security.filter.LogoutSuccessHandler;
import com.plum.admin.config.security.filter.PlumAccessDeniedHandler;
import com.plum.admin.config.security.filter.JwtAuthenticationFilter;
import com.plum.admin.config.security.mobile.SmsCodeAuthFilter;
import com.plum.admin.config.security.mobile.SmsCodeAuthenticationSecurityConfig;
import com.plum.admin.config.security.permission.MyFilterSecurityInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * TODO
 *
 * @author 子丶沫
 * @version 1.0
 * @date 2021/8/13 11:44
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private AuthenticationSuccessHandler successHandler;
    @Autowired
    private PlumAccessDeniedHandler accessDeniedHandler;
    @Autowired
    private AuthenticationFailureHandler failureHandler;
    @Autowired
    private LogoutSuccessHandler logoutSuccessHandler;
    @Autowired
    private IgnoredUrlsProperties ignoredUrlsProperties;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private MyFilterSecurityInterceptor filterSecurityInterceptor;
    @Autowired
    private SmsCodeAuthFilter smsCodeAuthFilter;
    @Autowired
    private SmsCodeAuthenticationSecurityConfig smsCodeAuthenticationSecurityConfig;
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry =
                http.authorizeRequests();
        //排除不需要拦截的请求
        ignoredUrlsProperties.getUrls().forEach(url-> registry.antMatchers(url).permitAll());
        registry.and()
               .formLogin()
               .loginPage("/common/noLogin")
               .loginProcessingUrl("/login")
               .permitAll()
               .successHandler(successHandler)
               .failureHandler(failureHandler)
               .and().headers().frameOptions().disable()
               .and()
               .logout()
               .logoutUrl("/logout")
               .logoutSuccessHandler(logoutSuccessHandler)
               .permitAll()
               .and()
               .authorizeRequests()
               .anyRequest().authenticated()
               .and()
               .cors().and().csrf().disable()
               .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
               .and()
               .exceptionHandling().accessDeniedHandler(accessDeniedHandler)
                .and()
                .addFilterBefore(filterSecurityInterceptor, FilterSecurityInterceptor.class)
                .apply(smsCodeAuthenticationSecurityConfig).and()
                .addFilterBefore(smsCodeAuthFilter, UsernamePasswordAuthenticationFilter.class)
                .addFilter(new JwtAuthenticationFilter(authenticationManager(),redisTemplate));

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
}
